<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['adminid']) || time() > $_SESSION['adminovertime'] || empty($_SESSION['adminovertime']) ) {

    		$this->redirect('/admin.php/Common/login');
    	}

        session('adminovertime',(time()+60*60*C('adminovertime')));

    }


    /**
     * [feedback 留言反馈]
     * @return [type] [description]
     */
    public function feedback(){
    	$m_feedback = M('Feedback');
    	$m_user = M('User');

    	$count = $m_feedback->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->adminshow();

        $feedbackList = $m_feedback->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($feedbackList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userAccount = $m_user->where($where_user)->getfield('account');
        	$feedbackList[$key]['account'] = $userAccount;
        }

        $this->assign('page',$show);
        $this->assign('feedbackList',$feedbackList);
    	$this->display();
    }


    /**
     * [reply 留言详情页面]
     * @return [type] [description]
     */
    public function reply(){
    	$id = $_REQUEST['id'];

    	$m_feedback = M('feedback');

    	$where['id'] = $id;
    	$feedbackInfo = $m_feedback->where($where)->find();

        $imgurl_arr = array();
        if (!empty($feedbackInfo['pic_url'])) {
            $imgurl_arr = explode(',', $feedbackInfo['pic_url']);
        }
        
        $this->assign('imgurl_arr',$imgurl_arr);
    	$this->assign('feedbackInfo',$feedbackInfo);
    	$this->display();
    }


    /**
     * [ctrl_reply 留言回复执行函数]
     * @return [type] [description]
     */
    public function ctrl_reply(){
    	if (IS_AJAX) {
    		$id = $_REQUEST['id'];
    		$content = trim($_REQUEST['content']);

    		$m_feedback = M('Feedback');

    		$where['id'] = $id;
    		$feedbackInfo = $m_feedback->where($where)->find();

    		if (!empty($feedbackInfo['feedback'])) {
    			$this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '已回复，请勿重复操作'
                ));
    		}else{
    			$save['feedback'] = $content;
    			$res = $m_feedback->where($where)->save($save);
    			if ($res) {
    				$this->ajaxReturn(array(
	                    'code'=>1,
	                    'msg' => '操作成功'
	                ));
    			}else{
    				$this->ajaxReturn(array(
                    	'code'=>0,
	                    'msg' => '操作失败'
	                ));
    			}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    public function ctrl_deleteFeedback(){
        $id = $_REQUEST['id'];

        $m_feedback = M('Feedback');

        $where['id'] = $id;
        $feedbackInfo = $m_feedback->where($where)->find();
        if (empty($feedbackInfo['feedback'])) {
            $this->ajaxReturn(array(
                'code'=>1,
                'msg' => '已删除管理员留言，请勿重复操作'
            ));
        }else{
            $save['feedback'] = '';
            $res = $m_feedback->where($where)->save($save);
            if ($res) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '操作成功'
                ));
            }else{
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '操作失败'
                ));
            }
        }
    }


    /**
     * [news 新闻公告]
     * @return [type] [description]
     */
    public function news(){
    	$m_news = M('News');

    	$count = $m_news->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->adminshow();

        $newsList = $m_news->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);
        $this->assign('newsList',$newsList);
    	$this->display();
    }


    /**
     * [ctrl_addNews 发布新闻公告]
     * @return [type] [description]
     */
    public function ctrl_addNews(){
    	if (IS_AJAX) {
    		$title = trim($_REQUEST['title']);
    		$content = trim($_REQUEST['content']);
    		$istop = trim($_REQUEST['istop']);

    		$m_news = M('News');

    		if (empty($title) || empty($content)) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '信息不完整，请完善'
                ));
    		}else{
	    		if (empty($istop)) {
	    			$add['top'] = 0;
	    		}else{
	    			$add['top'] = 1;
	    		}

	    		$where['title'] = $title;
	    		$newsInfo = $m_news->where($where)->find();
	    		if ($newsInfo['id']) {
	    			$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已添加，请勿重复操作'
	                ));
	    		}else{
	    			$add['title'] = $title;
	    			$add['content'] = $content;
	    			$add['createtime'] = time();

	    			$res = $m_news->add($add);
	    			if ($res) {
	    				$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '发布成功'
		                ));
	    			}else{
	    				$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '发布失败'
		                ));
	    			}
	    		}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    /**
     * [newsDetail 新闻详情]
     * @return [type] [description]
     */
    public function newsdetail(){
    	$id = $_REQUEST['id'];

    	$m_news = M('News');

    	$where['id'] = $id;
    	$newInfo = $m_news->where($where)->find();

    	$this->assign('newInfo',$newInfo);
    	$this->display();
    }


    /**
     * [ctrl_deleteNews 删除新闻公告]
     * @return [type] [description]
     */
    public function ctrl_deleteNews(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_news = M('News');

    		$where['id'] = $id;
    		$newsInfo = $m_news->where($where)->find();
    		if (empty($newsInfo['id'])) {
    			$this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '已删除'
                ));
    		}else{
    			$res = $m_news->where($where)->delete();
				if ($res) {
					$this->ajaxReturn(array(
	                    'code'=>1,
	                    'msg' => '删除成功'
	                ));
				}else{
					$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '删除失败'
	                ));
				}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    /**
     * [ctrl_cancelTop 取消新闻公告置顶]
     * @return [type] [description]
     */
    public function ctrl_cancelTop(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_news = M('News');

    		$where['id'] = $id;
    		$newsInfo = $m_news->where($where)->find();
    		if (empty($newsInfo['id'])) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '新闻公告不存在'
                ));
    		}else{
    			if ($newsInfo['top'] == 0) {
    				$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已取消置顶，请勿重复操作'
	                ));
    			}else{
    				$save['top'] = 0;
    				$res = $m_news->where($where)->save($save);
    				if ($res) {
    					$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '取消置顶成功'
		                ));
    				}else{
    					$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '取消置顶失败'
		                ));
    				}
    			}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    /**
     * [ctrl_top 新闻公告置顶]
     * @return [type] [description]
     */
    public function ctrl_top(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_news = M('News');

    		$where['id'] = $id;
    		$newsInfo = $m_news->where($where)->find();
    		if (empty($newsInfo['id'])) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '新闻公告不存在'
                ));
    		}else{
    			if ($newsInfo['top'] == 1) {
    				$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已置顶，请勿重复操作'
	                ));
    			}else{
    				$save['top'] = 1;
    				$res = $m_news->where($where)->save($save);
    				if ($res) {
    					$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '置顶成功'
		                ));
    				}else{
    					$this->ajaxReturn(array(
		                    'code'=>0,
		                    'msg' => '置顶失败'
		                ));
    				}
    			}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }
}